package pub.guoxin.oops.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.google.common.collect.Lists;
import com.querydsl.core.annotations.QueryEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Email;
import org.mongodb.morphia.annotations.Entity;
import pub.guoxin.oops.enums.Authority;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Date;
import java.util.List;

@Data
@Entity
@QueryEntity
@ApiModel("用户模块")
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class User extends BaseEntity<String> {

    /**
     * 公司Id
     */
    @ApiModelProperty(value = "用户名", required = true, hidden = true)
    private String companyId;

    @NotNull
    @Size(min = 4, max = 50)
    @ApiModelProperty(value = "用户名", required = true, hidden = true)
    private String username;

    @NotNull
    @Size(min = 4, max = 100)
    @ApiModelProperty(value = "密码", required = true, hidden = true)
    private String password;

    @NotNull
    @Size(min = 2, max = 50)
    @ApiModelProperty(value = "姓", required = true, hidden = true)
    private String firstname;

    @NotNull
    @Size(min = 2, max = 50)
    @ApiModelProperty(value = "名", required = true, hidden = true)
    private String lastname;

    @Email
    @NotNull
    @Size(min = 4, max = 50)
    @ApiModelProperty(value = "邮箱", required = true, hidden = true)
    private String email;

    @NotNull
    @ApiModelProperty(value = "是否启用", required = true, hidden = true)
    private Boolean enabled;

    @NotNull
    @Size(min = 11, max = 11)
    @ApiModelProperty(value = "手机号", required = true, hidden = true)
    private String mobile;

    @NotNull
    @ApiModelProperty(value = "注册阶段", required = true, hidden = true)
    private Boolean firstLogin;

    @NotNull
    @ApiModelProperty(value = "最后密码更新时间", required = true, hidden = true)
    private Date lastPasswordResetDate;

    @ApiModelProperty(value = "权限", required = true, hidden = true)
    private List<Authority> authorities = Lists.newArrayList();

    public boolean add(Authority authority){
        return this.authorities.add(authority);
    }

    public User(String username, String password, String email) {
        this.username = username;
        this.password = password;
        this.email = email;
    }

    public Authority findFirstAuthorityEnum(){
        return authorities.get(0);
    }
}